package Run;

import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import Chess.BorderPane;
import Chess.Main;
import Chess.MoveStrategy;
import Chess.chessPiece;
import Chess.chessType;

public class blackPawnStrategy implements MoveStrategy {
        private int moveTimes = 0;
        Stage stage = new Stage();
        @Override
        public boolean move(chessPiece currentChessPiece, int[][] chess,int x,int y) {
        	
            //下一个坐标无棋子，进行移动
            if(chess[y][x]!=1){
                //第一次移动
                if(moveTimes==0){
                    if(currentChessPiece.getX()==x && currentChessPiece.getY()-y>=-2){
                        currentChessPiece.move(x,y);
                        moveTimes++;
                        return true;
                    }
                }
                else{
                    if(currentChessPiece.getX()==x && currentChessPiece.getY()-y==-1){
                        currentChessPiece.move(x,y);
                        //到达底部，升级
                        if(y==7)
                            changeType(currentChessPiece,x,y);
                        return true;
                    }
                }
            }
            //下一个坐标有棋子，吃子
            else{
                //符合吃子位置
                if((Math.abs(x-currentChessPiece.getX())==1 && currentChessPiece.getY()-y==-1)
                        ||(Math.abs(x-currentChessPiece.getX())==1 && currentChessPiece.getY()-y==2 && moveTimes ==0)){
                    currentChessPiece.move(x,y);
                    if(y==7)
                        changeType(currentChessPiece,x,y);
                    //返回成功吃到子
                    return true;
                }
            }
            
            return false;
        }

        public void changeType(chessPiece piece,int x,int y){
        	Pane pane = new Pane();
        	Text text1 = new Text("请输入点击想要变成的棋子");
        	text1.setLayoutX(0);
        	text1.setLayoutY(20);
        	Image image1 = new Image("images/blackRook.png");
        	ImageView imageview1 = new ImageView(image1);
        	imageview1.setLayoutX(20);
        	imageview1.setLayoutY(40);
        	imageview1.setFitWidth(90);
        	imageview1.setFitHeight(90);
        	imageview1.setOnMouseClicked(new EventHandler<MouseEvent>(){
				public void handle(MouseEvent e){
        			piece.getImageView().setImage(null);
        			for(int k =0;k<BorderPane.getBlackChessList().size();k++){
		                chessPiece piece1 = (chessPiece) BorderPane.getBlackChessList().get(k);
		                if(piece.equals(piece1)){
		                    BorderPane.getBlackChessList().remove(k);
		                    break;
		                }
		            }
        			chessPiece piece = new chessPiece(x,y,chessType.blackRook,new RookStrategy());
        	        BorderPane.getBlackChessList().add(piece);
        	        BorderPane.reshapeImageView(x,y,piece);
        	        Main.pane.getChildren().add(piece.getImageView());
        	        Main.back.setDisable(true);
        	        stage.close();        	        
        		}
        	});
        	
        	Image image2 = new Image("images/blackQueen.png");
        	ImageView imageview2 = new ImageView(image2);
        	imageview2.setLayoutX(110);
        	imageview2.setLayoutY(40);
        	imageview2.setFitWidth(90);
        	imageview2.setFitHeight(90);
        	imageview2.setOnMouseClicked(new EventHandler<MouseEvent>(){
			public void handle(MouseEvent e){
        			piece.getImageView().setImage(null);
        			for(int k =0;k<BorderPane.getBlackChessList().size();k++){
		                chessPiece piece1 = (chessPiece) BorderPane.getBlackChessList().get(k);
		                if(piece.equals(piece1)){
		                    BorderPane.getBlackChessList().remove(k);
		                    break;
		                }
		            }
        			chessPiece piece = new chessPiece(x,y,chessType.blackQueen,new QueenStrategy());
        	        BorderPane.getBlackChessList().add(piece);
        	        BorderPane.reshapeImageView(x,y,piece);
        	        Main.pane.getChildren().add(piece.getImageView());
        	        Main.back.setDisable(true);
        	        stage.close();
        	        
        	       
        		}
        	});
        	
        	Image image3 = new Image("images/blackKnight.png");
        	ImageView imageview3 = new ImageView(image3);
        	imageview3.setLayoutX(200);
        	imageview3.setLayoutY(40);
        	imageview3.setFitWidth(90);
        	imageview3.setFitHeight(90);
        	imageview3.setOnMouseClicked(new EventHandler<MouseEvent>(){
				public void handle(MouseEvent e){
        			piece.getImageView().setImage(null);
        			for(int k =0;k<BorderPane.getBlackChessList().size();k++){
		                chessPiece piece1 = (chessPiece) BorderPane.getBlackChessList().get(k);
		                if(piece.equals(piece1)){
		                    BorderPane.getBlackChessList().remove(k);
		                    break;
		                }
		            }
        			chessPiece piece = new chessPiece(x,y,chessType.blackKnight,new KnightStrategy());
        	        BorderPane.getBlackChessList().add(piece);
        	        BorderPane.reshapeImageView(x,y,piece);
        	        Main.pane.getChildren().add(piece.getImageView());
        	        
        	        stage.close();
        	        
        	        Main.back.setDisable(true);
        		}
        	});
        	
        	Image image4 = new Image("images/blackBishop.png");
        	ImageView imageview4 = new ImageView(image4);
        	imageview4.setLayoutX(290);
        	imageview4.setLayoutY(40);
        	imageview4.setFitWidth(90);
        	imageview4.setFitHeight(90);
        	imageview4.setOnMouseClicked(new EventHandler<MouseEvent>(){
				public void handle(MouseEvent e){
        			piece.getImageView().setImage(null);
        			for(int k =0;k<BorderPane.getBlackChessList().size();k++){
		                chessPiece piece1 = (chessPiece) BorderPane.getBlackChessList().get(k);
		                if(piece.equals(piece1)){
		                    BorderPane.getBlackChessList().remove(k);
		                    break;
		                }
		            }
        			chessPiece piece = new chessPiece(x,y,chessType.blackBishop,new BishopStrategy());
        	        BorderPane.getBlackChessList().add(piece);
        	        BorderPane.reshapeImageView(x,y,piece);
        	        Main.pane.getChildren().add(piece.getImageView());
        	        
        	        stage.close();
        	        
        	        Main.back.setDisable(true);
        		}
        	});
        	
        	pane.getChildren().addAll(text1,imageview1,imageview2,imageview3,imageview4);
        	Scene scene = new Scene(pane,400,150);
            stage.setScene(scene);
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.showAndWait();
            
        }
    }
